// pages/my/my.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [],
    goodsList: [],
    isLoading: false,
    count: 1,
    limitNum: 10,
    showBackToTop: false
  },

  getBanners: function () {
    wx.request({
      url: 'http://47.94.6.186:3001/api/banner/list',
      method: 'GET',
      success: (res) => {
        this.setData({
          bannerList: res.data.data
        })
      },
      fail: (err) => {
        console.log(err)
      }
    })
  },

  getGoods: function (callback) {
    this.data.isLoading = true
    this.setData({
      isLoading: this.data.isLoading
    })
    wx.request({
      url: 'http://47.94.6.186:3001/api/pro/list',
      method: 'GET',
      data: {
        count: this.data.count,
        limitNum: this.data.limitNum
      },
      success: (res) => {
        const newGoods = res.data.data.map(item => {
          return {
            ...item,
            currentPrice: (item.originprice * item.discount / 10).toFixed(2),
            originPriceFormatted: item.originprice.toFixed(2)
          }
        })

        const goodsList = [...this.data.goodsList, ...newGoods];

        if (callback) {
          setTimeout(() => {
            callback()
            this.setData({
              goodsList: newGoods
            })
            wx.showToast({
              title: '刷新成功',
              icon: "success"
            })
          }, 1000)
        } else {
          this.data.isLoading = false
          setTimeout(() => {
            this.setData({
              goodsList,
              isLoading: this.data.isLoading
            })
          }, 1000)
        }
      },
      fail: (err) => {
        console.log(err)
        wx.showToast({
          title: '获取数据失败',
          icon: "error"
        })
      }
    })
  },

  goToDetail: function (e) {
    const id = e.currentTarget.dataset.proid
    wx.navigateTo({
      url: `/pages/goods-detail/goods-detail?proid=${id}`,
    })
  },

  imageError: function (e) {
    console.log('image发生error事件，携带值为', e.detail.errMsg)
  },

  // 滚动到顶部函数
  scrollToTop() {
    wx.pageScrollTo({
      scrollTop: 0, // 滚动到页面顶部
      duration: 300 // 动画时长（毫秒），可选
    })
  },

  // 监听页面滚动，控制回到顶部按钮显示/隐藏
  onPageScroll(e) {
    // 当滚动距离超过500rpx时显示按钮
    if (e.scrollTop > 500) {
      this.setData({
        showBackToTop: true
      })
    } else {
      this.setData({
        showBackToTop: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getBanners()
    this.getGoods()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getBanners()

    this.data.count = 1
    this.setData({
      count: this.data.count
    })
    this.getGoods(function () {
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.data.count++
    this.setData({
      count: this.data.count
    })
    this.getGoods()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})